' camera2d - 2D Camera viewport type
' written by the Math Wizard
' April 2009

Type Camera 
    As Integer  cursorx, cursory        ' where the cursor is on the screen
    As Double   t, b, l, r              ' top, bottom, left, right edges
    Declare Constructor( _
        ByVal   t       As Double, _
        ByVal   b       As Double, _
        ByVal   l       As Double, _
        ByVal   r       As Double _
    )
    Declare Destructor()
    Declare Sub Zoom( _                 ' Zoom in or out
        ByVal   fac     As Double _
    )
    Declare Sub Pan( _                  ' Displace viewport
        ByVal   x       As Double, _
        ByVal   y       As Double _
    )
End Type


Constructor Camera( _
    ByVal   t       As Double, _
    ByVal   b       As Double, _
    ByVal   l       As Double, _
    ByVal   r       As Double _
)
' Sets up the initial camera view with rudimentary
' range-checking.

' Make sure that top > bottom and right > left
If t < b Then Swap t, b
If r < l Then Swap r, l

' TODO: code to correct the aspect ratio

With This
    .t = t : .b = b : .l = l : .r = r
End With

End Constructor

Destructor Camera()

' Invalidates this viewport and returns us to console mode.

With This
    .t = 0
    .b = 0
    .l = 0
    .r = 0
End With

End Destructor

Sub Camera.Zoom( _
    ByVal   fac     As Double _
)
' pulls in or out of the current viewpoint.
' If 0 < fac < 1 then zoom out
'    fac => 1    then zoom in

Dim As Double   w, h                    ' width & height of camera viewport
Dim As Double   mx, my                  ' the middle of the screen

w = This.r - This.l                     ' get old camera information
h = This.t - This.b
mx = (This.l + This.r) / 2
my = (This.t + This.b) / 2

w = w * fac                             ' Calculate the new width & height
h = h * fac
This.t = my + (h / 2)                   ' Adjust the camera
This.b = my - (h / 2)
This.l = mx - (w / 2)
This.r = mx + (w / 2)

End Sub
